#pragma once
#include<assert.h>

namespace cyq
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		typedef cyq::reverse_iterator<T&, T*, iterator> reverse_iterator;
		typedef cyq::reverse_iterator<const T&,const T*, const_iterator> const_reverse_iterator;

		reverse_iterator rbegin()
		{
			return end();
		}

		reverse_iterator rend()
		{
			return begin();
		}

		const_reverse_iterator rbegin() const
		{
			return end();
		}

		const_reverse_iterator rend() const
		{
			return begin();
		}

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		vector()
		{}

		//用迭代区间构造
		template<class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}

		vector(size_t n, const T& x = T())
		{
			resize(n, x);
		}

		vector(const vector<T>& v)
		{
			_start = new T[v.capacity()];
			for (size_t i = 0; i < v.size(); i++)
			{
				_start[i] = v._start[i];
			}
			_finish = _start+v.size();
			_endofstorage = _start + v.capacity();
		}

		size_t size()
		{
			return _finish-_start;
		}

		size_t capacity()
		{
			return  _endofstorage-_start;
		}

		void swap(const vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endofstorage, v._endofstorage);

		}

		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}

		~vector()
		{
			delete[] _start;
			_start = _finish = _endofstorage = nullptr;
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t sz = size();

				T* temp = new T[n];
				for (size_t i = 0; i < size(); i++)
				{
					temp[i] = _start[i];
				}
				delete[] _start;
				_start = temp;
				_finish = _start + sz;
				_endofstorage = _start + n;
			}
		}

		iterator insert(iterator pos, const T& x)
		{
			assert(pos >= _start && pos <= _finish);
			if (_finish == _endofstorage)
			{
				size_t len = pos - _start;

				size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(newcapacity);
				pos = _start + len;
			}
			iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				end--;
			}

			*pos = x;
			_finish++;

			return pos;
		}

		void push_back(const T& x)
		{
			insert(end(), x);
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start * *pos < _finish);
			iterator temp = pos;

			while (pos > _finish)
			{
				*pos = *(pos + 1);
				pos++;
			}
			_finish--;
			return temp;
		}

		void pop_back()
		{
			--_finish;
		}

		bool empty()
		{
			return _start == _finish;
		}

		T& operator[](size_t i)
		{
			assert(i < size());
			return _start[i];
		}

		const T& operator[](size_t i) const
		{
			assert(i < size());
			return _start[i];
		}

		void resize(size_t n, const T& x=T())
		{
			if (n < size())
			{
				_finish = _start + n;
			}
			reserve(n);
			while (_finish < _start + n)
			{
				*_finish = x;
				_finish++;
			}
		}


	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _endofstorage = nullptr;
	};
}
